package com.siyu.tickets.entity;

import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.Set;

import com.siyu.common.hibernate3.PriorityInterface;
import com.siyu.common.util.DateUtils;
import com.siyu.core.entity.SysLoginLog;
import com.siyu.tickets.entity.base.BaseAdminUser;

public class AdminUser extends BaseAdminUser implements PriorityInterface {

	private static final long serialVersionUID = -7898533319097252987L;

	/**
	 * 本地头像
	 */
	public static final short AVATAR_LOCAL = 0;

	/**
	 * 链接头像
	 */
	public static final short AVATAR_LINK = 1;

	public SysLoginLog getUserLaestLoginLog() {
		Set<SysLoginLog> set = getLoginLogs();
		if (set != null && set.size() > 0) {
			return set.iterator().next();
		} else {
			return null;
		}
	}

	public int getLaestOnLineMinute() {
		SysLoginLog log = getUserLaestLoginLog();
		int timout;
		if (log == null) {
			timout = 0;
		} else {
			timout = (int) ((log.getLogoutTime().getTime() - log.getLoginTime().getTime()) / 1000 / 60);
		}
		return timout;
	}

	public int getDayOnLineMinute() {
		Set<SysLoginLog> logs = getLoginLogs();
		Iterator<SysLoginLog> it = logs.iterator();
		SysLoginLog log;
		int timout = 0;
		while (it.hasNext()) {
			log = it.next();
			if (DateUtils.isToday(log.getLoginTime())) {
				timout += (int) ((log.getLogoutTime().getTime() - log.getLoginTime().getTime()) / 1000 / 60);
			}
		}
		return timout;
	}

	public int getWeekOnLineMinute() {
		Set<SysLoginLog> logs = getLoginLogs();
		Iterator<SysLoginLog> it = logs.iterator();
		SysLoginLog log;
		int timout = 0;
		while (it.hasNext()) {
			log = it.next();
			if (DateUtils.isThisWeek(log.getLoginTime())) {
				timout += (int) ((log.getLogoutTime().getTime() - log.getLoginTime().getTime()) / 1000 / 60);
			}
		}
		return timout;
	}

	public int getMonthOnLineMinute() {
		Set<SysLoginLog> logs = getLoginLogs();
		Iterator<SysLoginLog> it = logs.iterator();
		SysLoginLog log;
		int timout = 0;
		while (it.hasNext()) {
			log = it.next();
			if (DateUtils.isThisMonth(log.getLoginTime())) {
				timout += (int) ((log.getLogoutTime().getTime() - log.getLoginTime().getTime()) / 1000 / 60);
			}
		}
		return timout;
	}

	public int getYearOnLineMinute() {
		Set<SysLoginLog> logs = getLoginLogs();
		Iterator<SysLoginLog> it = logs.iterator();
		SysLoginLog log;
		int timout = 0;
		while (it.hasNext()) {
			log = it.next();
			if (DateUtils.isThisYear(log.getLoginTime())) {
				timout += (int) ((log.getLogoutTime().getTime() - log.getLoginTime().getTime()) / 1000 / 60);
			}
		}
		return timout;
	}

	public void forUser(SysUser u) {
		setId(u.getId());
	}

	public void init() {
	}

	public static Integer[] fetchIds(Collection<AdminUser> users) {
		if (users == null) {
			return null;
		}
		Integer[] ids = new Integer[users.size()];
		int i = 0;
		for (AdminUser u : users) {
			ids[i++] = u.getId();
		}
		return ids;
	}

	/**
	 * 用于排列顺序。此处优先级为0，则按ID升序排。
	 */
	public Number getPriority() {
		return 0;
	}

	/**
	 * 是否是今天。根据System.currentTimeMillis() / 1000 / 60 / 60 / 24计算。
	 * 
	 * @param date
	 * @return
	 */
	public static boolean isToday(Date date) {
		long day = date.getTime() / 1000 / 60 / 60 / 24;
		long currentDay = System.currentTimeMillis() / 1000 / 60 / 60 / 24;
		return day == currentDay;
	}

	/* [CONSTRUCTOR MARKER BEGIN] */
	public AdminUser() {
		super();
	}

	/**
	 * Constructor for primary key
	 */
	public AdminUser(java.lang.Integer id) {
		super(id);
	}

	/**
	 * Constructor for required fields
	 */
	public AdminUser(java.lang.Integer id, java.lang.String idCard, java.lang.String gender, SysUser user) {
		super(id, idCard, gender, user);
	}

	/* [CONSTRUCTOR MARKER END] */

}